import './tboot';
import fs from 'fs';
import uuid from 'uuid/v4';
import expect from 'expect';
import fetch from 'node-fetch';
import { loadConfig, Config } from 'src/app';
import { llog } from 'src/util/logger';
import { getRandomArbitrary, sleep } from 'src/util/tools';

let siteLocal = `http://localhost:${Config.getHttpPort()}`;
let siteLocalDocker = `http://localhost:${10000 + Config.getHttpPort()}`;

let start = async () => {
	loadConfig();
	llog('!!test...', Config.getRedis());

	let testSites = [siteLocal, siteLocalDocker];
	testSites = testSites.reverse();
	let userInfo = await testRegisterUser(testSites[0]);
	await sleep(1000);
	await testSyncUser(testSites[1], userInfo);

	// userInfo = await testRegisterUser(testSites[1]);
	// await sleep(1000);
	// await testSyncUser(testSites[0], userInfo);
};

let testRegisterUser = async site => {
	llog('register...');
	let url, res, restext, resjson;
	let phone = Math.floor(getRandomArbitrary(13000000, 14000000)).toString();
	let id = uuid();
	console.log('test sync src', site);
	console.log(`>>register ${id}, ${phone}`);
	let info = {
		name: getRandomArbitrary(10000, 20000).toString(),
		phone,
		id,
		email: 'abc@qq.com',
		password: 'cbdgw',
	};
	let body = JSON.stringify(info);

	url = `${site}/user/notify`;
	res = await fetch(url, {
		method: 'post',
		body,
	});
	resjson = await res.json();
	console.log(resjson);

	url = `${site}/user/register`;
	res = await fetch(url, {
		method: 'post',
		body,
	});
	resjson = await res.json();
	console.log(resjson);
	expect(resjson.genVerifyCode).toBeDefined();
	let verifyCode = resjson.genVerifyCode;

	// 频繁请求验证码
	res = await fetch(url, {
		method: 'post',
		body,
	});
	resjson = await res.json();
	console.log(resjson);
	expect(resjson.error).toEqual('请求验证码太快');

	// 使用错误验证码
	res = await fetch(url + '?verifyCode=234', {
		method: 'post',
		body,
	});
	resjson = await res.json();
	console.log(resjson);
	expect(resjson.error).toBe('验证码不匹配');

	// 使用正确验证码
	res = await fetch(url + '?verifyCode=' + verifyCode, {
		method: 'post',
		body,
	});
	resjson = await res.json();
	console.log(resjson);
	expect(resjson.error).toBeUndefined();
	expect(resjson.msg).toBe('注册成功');

	// 获取刚注册用户
	url = `${site}/user/${id}`;
	// url = `https://localhost:${Config.getSecurePort()}/user/${id}`;
	// url = `https://10.1.1.206:${Config.getSecurePort()}/user/${id}`;
	const https = require('https');
	const agent = new https.Agent({
		rejectUnauthorized: false,
	});
	res = await fetch(url, {
		// agent,
	});
	resjson = await res.json();
	console.log(resjson);
	expect(resjson.phone).toBe(phone);
	expect(resjson.name).toBe(info.name);
	info.id = id;
	return info;
};

let testSyncUser = async (site, userInfo) => {
	let url = `${site}/user/${userInfo.id}`;
	console.log('test sync result', url);
	let res, restext, resjson;
	res = await fetch(url, {
		// agent,
	});
	resjson = await res.json();
	console.log(resjson);
	expect(resjson.phone).toBe(userInfo.phone);
	expect(resjson.name).toBe(userInfo.name);
};

start();
